/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.portalnahouby.implementation.daoimplementation;

import cz.muni.fi.pa165.portalnahouby.implementation.daointerface.VisitDAO;
import cz.muni.fi.pa165.portalnahouby.implementation.entity.Forest;
import cz.muni.fi.pa165.portalnahouby.implementation.entity.Visit;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * Implementation of VisitDAO interface
 *
 * @author Filip Kiss
 */
@Repository
@Transactional
public class VisitDAOImpl implements VisitDAO {
    
@PersistenceContext
    private EntityManager em;
    
    @Override
    public void addVisit(Visit visit) {
        if (visit == null) {
            throw new IllegalArgumentException("Visit can not be null.");
        }

        if (visit.getId() != null) {
            throw new IllegalArgumentException("Id of visit must be null");
        }

        em.merge( visit.getPicker());
        em.merge( visit.getForest());
        em.persist(visit);
    }

    @Override
    public void editVisit(Visit visit) {
        if (visit == null) {
            throw new IllegalArgumentException("Visit can not be null.");
        }

        if (visit.getId() == null) {
            throw new NullPointerException("Id of visit can not be null.");
        }

        em.merge(visit);
    }

    @Override
    public void deleteVisit(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("Id of visit can not be null.");
        }

        Visit visit = em.find(Visit.class, id);

        em.remove(visit);
    }

    @Override
    public Visit findVisit(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("Id of visit can not be null.");
        }

        return em.find(Visit.class, id);
    }

    @Override
    public List<Visit> getAllVisits() {
        return em.createQuery("SELECT v FROM Visit v", Visit.class).getResultList();
    }

    @Override
    public List<Visit> getListOfForestByDate(Forest forest) {
        TypedQuery<Visit> query = em.createQuery("SELECT v FROM Visit v WHERE v.forest = :forest ORDER BY visitDate", Visit.class);
        query.setParameter("forest", forest);
        return query.getResultList();
    }

    @Override
    public List<Visit> getListOfForestByPicker(Forest forest) {
        TypedQuery<Visit> query = em.createQuery("SELECT v FROM Visit v WHERE v.forest = :forest ORDER BY picker", Visit.class);
        query.setParameter("forest", forest);
        return query.getResultList();
    }
    
    @Override
    public List<Visit> getListByForest() {
        TypedQuery<Visit> query = em.createQuery("SELECT v FROM Visit v ORDER BY forest", Visit.class);
        return query.getResultList();
    }
    
    
    
}
